package com.zingrow.web.jms;

import java.io.File;
import java.io.FileInputStream;
import java.io.Serializable;
import java.util.Properties;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;

import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zingrow.common.utils.MongoHelper;
import com.zzyq.dataManager.bean.MessageText;

/**
 * JMS消息生产者
 * 
 * @author czq
 * @date 2017年3月14日 上午9:27:53
 */
public class JMSProducer {
	private static Logger logger = LoggerFactory.getLogger(JMSProducer.class);

	// 连接工厂
	public static ConnectionFactory connectionFactory;

	// 连接
	public static Connection connection = null;

	// 会话 接受或者发送消息的线程
	public static Session session = null;

	// 消息的目的地
	public static Destination destination;

	// 消息生产者
	public static MessageProducer messageProducer;

	public static String url = "";

	public static final String queue = "data";

	public static void main(String[] args) throws Exception {
		MessageText message = new MessageText();
//		message.setIfmId("290");
//		message.setArticleId("54510b626a7abb1e47d94f00845a4bab");
//		message.setTitle("开发环境调试——阿杜39+7勇士力克骑士 总分4-1夺总冠军");
//		message.setType(0);
//		JMSProducer.sendMessage(message);
		message.setIfmId("0");
		message.setArticleId("0");
		 message.setTitle("月报2017-10-01");
		message.setType(1);
		message.setRepId(1098);
		message.setModuleSort("1,2,3,4,5,6,7,8");
		JMSProducer.sendMessage(message);
//		message.setIfmId("0");
//		message.setArticleId("0");
		// message.setTitle("日报2017-08-14");
//		message.setType(1);
//		message.setRepId(420);
//		message.setModuleSort("13,14,15,16,18,19");
//		JMSProducer.sendMessage(message);
		connection.close();
	}

	private static void init() {
		if (StringUtils.isBlank(url)) {
			try {
				FileInputStream fis = new FileInputStream(
						new File(MongoHelper.class.getResource("/").getPath(), "properties/activemq.properties"));
				Properties properties = new Properties();
				properties.load(fis);
				String activemq_url = properties.getProperty("activemq_url");
				url = activemq_url;
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}

	public synchronized static void sendMessage(Serializable message) throws Exception {
		if (null != message) {
			Thread.sleep(3000);
			init();
			try {
				// 实例化连接工厂
				if (null == connectionFactory) {
					connectionFactory = new ActiveMQConnectionFactory(ActiveMQConnection.DEFAULT_USER,
							ActiveMQConnection.DEFAULT_PASSWORD, url);
				}
				// 通过连接工厂获取连接
				if (null == connection) {
					connection = connectionFactory.createConnection();
				}
				// 启动连接
				connection.start();
				// 创建session
				if (null == session) {
					session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
				}
				// 创建一个消息队列
				if (null == destination) {
					destination = session.createQueue(queue);
				}
				// 创建消息生产者
				if (null == messageProducer) {
					messageProducer = session.createProducer(destination);
				}
				ObjectMessage objectMessage = session.createObjectMessage();
				objectMessage.setObject(message);
				// 通过消息生产者发出消息
				messageProducer.send(objectMessage);
				// 发送消息
				session.commit();
			} catch (Exception e) {
				e.printStackTrace();
				logger.error(e.toString());
			}
		}
	}
}
